package com.dyz.LeetCodeforSwordFingerOffer;

public class ReverseList24 {
    public static void main(String[] args) {
        ListNode root = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        node4.next = node5;
        node3.next = node4;
        node2.next = node3;
        root.next = node2;
        ListNode listNode = new ReverseList24().reverseList(root);
    }
    //思路：使用前后指针
    public ListNode reverseList(ListNode head) {
        //处理特殊情况
        if(head==null||head.next==null){
            return head;
        }
        ListNode dummy = new ListNode(0);
        ListNode cur = head;
        ListNode body = head.next;
        while (body!=null){
            cur.next = dummy.next;
            dummy.next = cur;
            cur = body;
            body = body.next;
        }
        //最后一个
        if(cur!=null){
            cur.next = dummy.next;
            dummy.next = cur;

        }
        return dummy.next;
    }
}
